# -------------------------------------------------------------------
# Purpose: Creates Table B46
# Author:  Max Posch, 25/07/2025
# Usage:   Source this script to generate the table.
# -------------------------------------------------------------------
# Check that required paths exist
stopifnot(dir.exists(pdataconfanalysis))
stopifnot(dir.exists(poutputappendix))


# Load data
load(file.path(pdataconfanalysis, "patentLevel19001940.RData"))


# Create table
a <- nrow(patentLevel19001940[no_of_inventors == 1])
aa <- round(a / nrow(patentLevel19001940) * 100, 2)
b <- nrow(patentLevel19001940[no_of_inventors > 1 & entropy_patent_namelast_mp_adjp == 0])
bb <- round(b / nrow(patentLevel19001940) * 100, 2)
c <- nrow(patentLevel19001940[no_of_inventors > 1 & entropy_patent_namelast_mp_adjp > 0])
cc <- round(c / nrow(patentLevel19001940) * 100, 2)
data <- data.frame(
  Description = c(
    "$1$ inventor",
    "$>1$ inventors and $1$ distinct surname",
    "$>1$ inventors and $>1$ distinct surnames"
  ),
  Number = c(a, b, c),
  `Shares (%)` = c(aa, bb, cc)
)

tablename <- file.path(poutputappendix, "tableB46.tex")
print.xtable_custom(data, file = tablename)

cat("Table B46 saved to:", tablename, "\n")
